package algorithm.List.Queue;

/**
 * 数组实现循环队列
 */
public class MyCircularQueue {

    private int head;
    private int tail;
    private int size;
    private int capacity;

    private int[] queue;

    public MyCircularQueue(int k){
        this.capacity=k;
        queue=new int[k];
        this.head=this.tail=this.size=0;
    }
    public boolean isEmpty(){
        return size==0;
    }
    public boolean isFull(){
        return size==capacity;
    }
    //向队列加数
    //如果满了返回false
    //如果没满就加入元素
    public boolean enQueue(int val){
        if(isFull()) return false;
        queue[tail]=val;
        tail=tail==capacity-1?0:tail+1;
        size++;
        return true;
    }

    public boolean deQueue(){
        if(isEmpty()) return false;
        head=head==capacity-1?0:head+1;
        size--;
        return true;
    }

    public int front(){
        if(isEmpty()) return -1;
        return queue[head];
    }

    public int rear(){
        if(isEmpty()) return -1;
        int last=tail==0?capacity-1:tail-1;
        return queue[last];
    }

}
